package aixin

import (
	"flag"
	"fmt"
	"image"
	"image/color"
	"image/gif"
	"math"
	"os"
	"strings"
	"time"
)

// 申明画板的颜色组
var palette = []color.Color{color.White, color.Black, color.RGBA{0xff, 0x00, 0x00, 0xff}}

func Aixin() {
	const (
		nframes = 50  // GIF的帧数
		delay   = 10  // 每帧间的时间间隔
		size    = 400 // 图片大小
	)

	a := 0.0
	anim := gif.GIF{LoopCount: nframes} // GIF文件对象

	for i := 0; i < nframes; i++ {
		rect := image.Rect(0, 0, size+1, size+1)
		img := image.NewPaletted(rect, palette) // 新建一个画板，指定宽度、高度和调色板只要色彩

		for x := -2.0; x < 2.0; x += 0.0001 {
			f1 := math.Pow(math.Abs(x), 2.0/3)
			f2 := math.E / 4 * math.Sqrt(math.Pi-math.Pow(x, 2.0)) * math.Sin(math.Pi*a*x)
			if math.IsNaN(f2) {
				f2 = 0
			}
			y := -(f1 + f2)
			img.SetColorIndex(int(x*size/4)+200, int(y*size/4)+250, 2)
		}
		a++

		anim.Delay = append(anim.Delay, delay)
		anim.Image = append(anim.Image, img)
	}

	var filename = "test.gif"
	if len(os.Args) > 1 {
		filename = os.Args[1] + ".gif"
	}
	file, _ := os.Create(filename)
	defer file.Close()

	gif.EncodeAll(file, &anim)
}

func Get_love() {
	var head string
	var tail string
	var MYWORD string
	var sep string
	var zoom float64
	flag.StringVar(&head, "head", "There are some words I want to tell you:段芃羽和袁梦涓天下第一好", "A sentence printed on the head") // 添加开头要写的话
	flag.StringVar(&tail, "tail", "\t\t\t\t--- Resss Productions", "A sentence printed on the tail")                        // 添加结尾要写的话
	flag.StringVar(&MYWORD, "words", "dpy love ymj!", "The words you want to say")                                          // 爱心中的内容
	flag.StringVar(&sep, "sep", " ", "The separator")
	flag.Float64Var(&zoom, "zoom", 1.0, "Zoom setting")
	flag.Parse()

	chars := strings.Split(MYWORD, sep)

	time.Sleep(time.Duration(1) * time.Second)
	fmt.Println(head)
	fmt.Println()
	time.Sleep(time.Duration(1) * time.Second)
	for _, char := range chars {
		allChar := make([]string, 0)

		for y := 12 * zoom; y > -12*zoom; y-- {
			lst := make([]string, 0)
			lstCon := ""
			for x := -30 * zoom; x < 30*zoom; x++ {
				x2 := float64(x)
				y2 := float64(y)
				formula := math.Pow(math.Pow(x2*0.04/zoom, 2)+math.Pow(y2*0.1/zoom, 2)-1, 3) - math.Pow(x2*0.04/zoom, 2)*math.Pow(y2*0.1/zoom, 3)
				if formula <= 0 {
					index := int(x) % len(char)
					if index >= 0 {
						lstCon += string(char[index])
					} else {
						lstCon += string(char[int(float64(len(char))-math.Abs(float64(index)))])
					}

				} else {
					lstCon += " "
				}
			}
			lst = append(lst, lstCon)
			allChar = append(allChar, lst...)
		}

		for _, text := range allChar {
			fmt.Printf("%s\n", text)
			time.Sleep(time.Duration(200) * time.Millisecond) // 每个字符生成的时间为0.2秒
		}
	}
	time.Sleep(time.Duration(1) * time.Second)
	fmt.Println("\t\t\t\t", tail)

}
